package com.jinlia.show.modules.system.controller.sys;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.jinlia.show.modules.base.model.po.SysRolePO;
import com.jinlia.show.modules.base.model.vo.SysMenuVO;
import com.jinlia.show.modules.base.system.entity.SysMenu;
import com.jinlia.show.modules.system.model.Option;
import com.jinlia.show.modules.system.service.SysMenuService;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

@RestController
@RequestMapping("menu")
public class SysMenuController {
    @Autowired
    private SysMenuService menuService;


    @PostMapping("tree")
    public ResponseEntity listMenuOptions(@RequestBody SysMenu menu) {
        List<SysMenuVO> treeVO = menuService.getTreeVO(menu);
        treeVO =treeVO.stream().sorted(Comparator.comparing(SysMenuVO::getMenuOrder)).collect(Collectors.toList());
        return ResponseEntity.ok(treeVO);
    }
    @PostMapping("add")
    public ResponseEntity add(@RequestBody SysMenu menu) {
        menu = menuService.add(menu);
        return ResponseEntity.ok(menu);
    }
    @PostMapping("update")
    public ResponseEntity update(@RequestBody SysMenu menu) {
        menu = menuService.updateMenu(menu);
        return ResponseEntity.ok(menu);
    }
    @GetMapping("my")
    public ResponseEntity getCurrentMenuTree() {
        List<SysMenuVO> treeVO = menuService.getCurrentMenuTree();
        treeVO = treeVO.stream().sorted(Comparator.comparing(SysMenuVO::getMenuOrder)).collect(Collectors.toList());
        return ResponseEntity.ok(treeVO);
    }

    @GetMapping("/button/{menuId}")
    public ResponseEntity<IPage<SysMenu>> getMenuButtonPage(@PathVariable("menuId") Long menuId) {
        return ResponseEntity.ok(menuService.getMenuButtonPage(menuId));
    }
}
